package com.example.amoxicilin.null_test.main;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Status;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;


public class AnswerSubmitActivity extends AppCompatActivity {

    EditText editText;
    Button button;
    int usernumber;
    String content;
    Status status;
    int answernumber;
    int questionnumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_submit);

        Bundle bundle=getIntent().getExtras();
        usernumber=bundle.getInt("usernumber");
        questionnumber=bundle.getInt("QuestionNumber");
        Log.i("tag",usernumber+"");

        editText=(EditText)findViewById(R.id.edit_answer);
        button=(Button)findViewById(R.id.answer_submit_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content=editText.getText().toString();

                new AlertDialog.Builder(AnswerSubmitActivity.this)
                        .setTitle("系统消息")
                        .setMessage("确定要提交吗？")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                String path="http://120.24.94.172/JsonTestServer/jsonTest?type1=insert&type2=answer&number="+questionnumber+"&pnumber="+usernumber
                                        +"&content2="+content+"&upvote=0&date=1970-03-22&label1=1";
                                String jsonString = HttpUtils.getJsonContent(path);
                                Log.i("nimabi", "onClick: "+path);
                                status= JsonTools.getStatus(null,jsonString);
                                if (status.getStatusId()==1)
                                {
                                    new android.app.AlertDialog.Builder(AnswerSubmitActivity.this)
                                            .setTitle("系统消息")
                                            .setMessage("提交成功！").setPositiveButton("确认",new DialogInterface.OnClickListener(){
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            finish();
                                        }}).show();
                                }
                                else{
                                    new android.app.AlertDialog.Builder(AnswerSubmitActivity.this)
                                            .setTitle("系统消息")
                                            .setMessage("提交失败！").setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {}}).show();
                                }
                            }})
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .show();
            }
        });
    }
}
